
package bancoDeDados;

/**
 *
 * @author Gean
 */
import java.sql.*;
import java.util.ArrayList;

public class Conexao{
    
    private static Connection conexao;
    private Statement smt;

    public Conexao(){
        conexao = conecta();
    }
    
    public Connection conecta(){
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/bd_rp_2.1";
        String login = "root";
        String senha = "e02c50";
        
        try{
           Class.forName(driver);
            Conexao.setConexao(DriverManager.getConnection(url, login, senha));
            System.out.println("Conexão bem sucedida!");
        } catch(Exception e){
            e.printStackTrace();
        }
        return Conexao.getConexao();
    }
    
    public void insert(String script) throws SQLException{
            smt = conexao.createStatement();
            smt.executeUpdate(script);
            
       }
     
     public boolean select(String script){
         ArrayList<String> pega = new ArrayList(); 
         int cont=0;
         try{
            smt = conexao.createStatement();
            ResultSet query = smt.executeQuery(script);
            
            while(query.next()) {    //move o curso de registros
            
                   pega.add(query.getString("login"));
                   pega.add(query.getString("senha"));
                   
                }
             for (int i = 0; i < pega.size(); i++) {
                 if(pega.get(i) != null){
                    cont++; 
                   }
             }
             if(cont == 2){   
                return true;
             }else{
                return false;
             }
          }catch(SQLException e){
            System.out.println("erro sql conexao: "+e);
            return false;
        }
     }
     
    public static Connection getConexao() {
        return conexao;
    }

    public static void setConexao(Connection aConexao) {
        conexao = aConexao;
    }    
    public static void main(String[] args) {
        Conexao c = new Conexao();
        
    }
}    
